layui.use(['form', 'layer', 'jquery'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    //点击验证码图片修改验证码
    $('#verificationCode').click(getVerificationCode);


    $(".loginBody .seraph").click(function () {
        layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧",
            {
                time: 5000
            });
    });

    //登录按钮
    form.on("submit(login)",
        function (data) {

            var btn = $(this);
            //设置登录按钮 为不可点击
            btn.text("登录中...").attr("disabled", "disabled").addClass("layui-disabled");

            $.post("/Login/ProcessLogin",
                data.field,
                function (rs) {

                    rs = $.parseJSON(rs);
                    //设置登录按钮 恢复可点击 在前端防止重复点击

                    btn.text("登录").attr('disabled', false).removeClass('layui-disabled');
                    //layer.msg(rs.Msg);
                    if (rs.Code !== 200) { //登录失败
                        layer.msg(rs.Msg);

                        //重新获取验证码
                        getVerificationCode();

                    } else { //登录成功

                        layer.msg(rs.Msg);
                        location.href = '/Sys/Index'; //跳转到系统首页
                    }
                });

            return false;
        });


    //获取验证码
    function getVerificationCode() {
        //思路改变src图片路径,就会重新发起验证码图片的请求
        var originalSrc = $('#verificationCode').attr('originalSrc');
        var nowDate = new Date();
        originalSrc = originalSrc + '?ver=' + nowDate.getHours() + nowDate.getMinutes() + nowDate.getSeconds();
        $('#verificationCode').attr('src', originalSrc);
    };

    //表单输入效果
    $(".loginBody .input-item").click(function (e) {
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    });

    $(".loginBody .layui-form-item .layui-input").focus(function () {
        $(this).parent().addClass("layui-input-focus");
    });

    $(".loginBody .layui-form-item .layui-input").blur(function () {
        $(this).parent().removeClass("layui-input-focus");
        if ($(this).val() != '') {
            $(this).parent().addClass("layui-input-active");
        } else {
            $(this).parent().removeClass("layui-input-active");
        }
    });
});
